package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "后台-套餐管理")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取套餐")
    public Result<SetmealVO> getSetmealById(@PathVariable Long id) {
        log.info("套餐ID: {}", id);
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    // 清理指定分类套餐下的缓存
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId")
    @PostMapping
    @ApiOperation("新增套餐")
    public Result<String> addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐: {}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    @ApiOperation("分页查询套餐")
    @GetMapping("/page")
    public Result<PageResult> list(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询套餐: {}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.getSetmealPage(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result<String> updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐: {}", setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("套餐起售停售")
    @PostMapping("/status/{status}")
    public Result<String> updateSetmealStatus(@PathVariable Integer status, Long id) {
        log.info("套餐起售停售: {}, {}", status, id);
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealService.updateSetmealStatus(setmeal);
        return Result.success();
    }

    // 清理setmeal下所有缓存
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("批量删除")
    @DeleteMapping
    public Result<String> deleteSetmeal(@RequestParam List<Long> ids) {
        log.info("批量删除: {}", ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }
}
